package com.jabo.douban.demo.test;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

/**
 * @anthor JZheng3
 * @Time: 2020/3/6
 */
public class NineGridPictureLayoutImpl extends NineGridPictureLayout {

    public NineGridPictureLayoutImpl(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public NineGridPictureLayoutImpl(Context context) {
        super(context);
    }

    public NineGridPictureLayoutImpl(Context context, AttributeSet attrs) {
        super(context, attrs);

//        ViewPropertyAnimator animate = animate();
//        ValueAnimator valueAnimator = ValueAnimator.ofInt(100, 500);
//        ObjectAnimator objectAnimator = ObjectAnimator.ofObject()
    }

    @Override
    public void loadContentOneView(final ImageView childView, String dataInfo, int position, final int parentWidthSize) {
        Glide.with(getContext()).asBitmap().load(dataInfo).into(new SimpleTarget<Bitmap>() {
            @Override

            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                int maxWidth = parentWidthSize * 2 / 3;
                int maxHeight = parentWidthSize;

                if (resource.getWidth() < maxWidth) {
                    maxWidth = resource.getWidth();
                    if (resource.getHeight() < maxHeight) {
                        maxHeight = resource.getHeight();
                    }
                } else {
                    maxHeight = resource.getHeight() * maxWidth / resource.getWidth();
                }

                setOneViewParam(childView, maxHeight, maxWidth);
                childView.setImageBitmap(resource);
            }
        });
    }

    @Override
    protected void loadContentMoreViews(final ImageView child, String dataInfo, int i) {
        Glide.with(getContext()).asBitmap().load(dataInfo).into(new SimpleTarget<Bitmap>() {
            @Override

            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                child.setImageBitmap(resource);
            }
        });
    }
}
